package TeacherAdmin;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class Test extends Init {
    /*
     登录
     */
    @ParameterizedTest
    @CsvFileSource(resources = "../test.csv")//要有对应的文件test.csv
    @Order(1)
    void login(String name,String password) throws InterruptedException {
        System.out.println("username:"+name);
        System.out.println("password:"+password);
        //输入账号密码
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(1) > div > div > input")).sendKeys(name);
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(2) > div > div > input")).sendKeys(password);
       //点击登录
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(3) > div > button")).click();
        webDriver.manage().window().maximize();
        sleep(3000);

        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://192.168.63.227:10001/digitrain/login/toIndex#/home",curUrl);
    }
    /*
    选择第一个装配体
     */
    @org.junit.jupiter.api.Test
    @Order(2)
    void selectTask() throws InterruptedException {
        //选择第一个装配体
        webDriver.findElement(By.cssSelector("#app > section > main > div.zw-layout-main-inner > div > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div.zw-home-case-list > div:nth-child(1)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        //获取主页URL
        sleep(3000);
        Set<String> handles = webDriver.getWindowHandles();
        String curHandle = "";
        for (String s:
                handles) {
            curHandle = s;
        }
        webDriver.switchTo().window(curHandle);//切换到当前的操作页面
        String curURL = webDriver.getCurrentUrl();
        //只判断url部分，去除querystring
        String[] urls = curURL.split("=");
        Assertions.assertEquals("http://192.168.63.227:10001/digitrain/login/toIndex#/case?id",urls[0]);

    }
    /*
        点击开始计时
     */
    @org.junit.jupiter.api.Test
    @Order(3)
    void clickTime() throws InterruptedException {
        //点击开始计时/重新计时
        webDriver.findElement(By.cssSelector("#\\35 72778704577495040 > div.zw-case-countdown-box > div.zw-case-countdown-btn-group > button > span")).click();
        sleep(3000);
        //获取当前的计时时间 秒
        String time = webDriver.findElement(By.cssSelector("#\\35 72778704577495040 > div.zw-case-countdown-box > div.zw-case-countdown-time > span > span:nth-child(5)")).getText();
        System.out.println("时间:"+time);
        Assertions.assertNotEquals("00",time);
    }
    /*
        查看解题思路视频
     */
    @org.junit.jupiter.api.Test
    @Order(4)
    void clickVideo() throws InterruptedException {
        //点击解题思路
        webDriver.findElement(By.cssSelector("#tab-tips")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        webDriver.findElement(By.cssSelector("#pane-tips > div > div > div.zw-case-tips-item-cover")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        String videoName = webDriver.findElement(By.cssSelector("body > div:nth-child(8) > div > div > header > span:nth-child(1)")).getText();
        System.out.println("视频名称:"+videoName);
        Assertions.assertNotNull(videoName);
        //关闭视频弹窗
        sleep(3000);
        webDriver.findElement(By.cssSelector("body > div:nth-child(8) > div > div > header > span:nth-child(2) > svg")).click();
    }
    /*
        验证上传文件校验
     */
    @Disabled
    @org.junit.jupiter.api.Test
    @Order(5)
    void verifyUpload() throws InterruptedException {
        //点击模拟上传全部文件校验
        webDriver.findElement(By.cssSelector("#app > section > main > div.zw-layout-main-inner > div > div.el-scrollbar.zw-case-left > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div.zw-case-left-footer > div.fake-upload-btn")).click();
        sleep(3000);
        //点击上传 todo:怎么上传文件？
        webDriver.findElement(By.cssSelector("#app > section > main > div.zw-layout-main-inner > div > div.zw-case-fake-upload > div.zw-case-fake-upload-btn > button")).sendKeys("C:\\Desktop.zip");
        //选择文件
        sleep(3000);
//        element.sendKeys("C:\\Users\\admin\\Desktop\\2.png");
//        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofHours(1));
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[6]/div/div/div/div[1]/text()")));

    }
    /*
     验证答案权限设置
     */

    @org.junit.jupiter.api.Test
    @Order(6)
    void clickAuthority() throws InterruptedException {
        //点击查看答案权限
        webDriver.findElement(By.cssSelector("#app > section > main > div.zw-layout-main-inner > div > div.el-scrollbar.zw-case-left > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div.zw-case-left-footer > div.zw-case-answer-toggle")).click();

        sleep(3000);
        //todo:怎么进行显示等待
//        WebDriverWait webDriverWait = new WebDriverWait(webDriver,Duration.ofHours(1));
//        //等待弹窗出现
//        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("#el-id-776-10 > div.answer-toggle-title")));
        //打开or关闭权限
        webDriver.findElement(By.xpath("/html/body/div[4]/div/div/div/div[2]/div/span")).click();
        //关闭弹出
        webDriver.findElement(By.cssSelector("body > div:nth-child(7) > div > div > header > button > i > svg")).click();
        sleep(1000);
    }
    /*
        验证下载参考答案
     */
    @org.junit.jupiter.api.Test
    @Order(7)
    void downAnswer(){
        //点击步骤1
        webDriver.findElement(By.cssSelector("#app > section > main > div.zw-layout-main-inner > div > div.el-scrollbar.zw-case-left > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div.zw-case-left-content > ul > li.el-menu-item.is-active")).click();
        //点击参考答案
        webDriver.findElement(By.cssSelector("#tab-answer")).click();
        //点击下载参考答案
        webDriver.findElement(By.cssSelector("#pane-answer > div > div:nth-child(1) > div > a > button > span")).click();
    }
    /*
        验证预览参考答案
     */
    @org.junit.jupiter.api.Test
    @Order(8)
    void clickAnswer() throws InterruptedException {
        //点击预览参考答案
        webDriver.findElement(By.cssSelector("#pane-answer > div > div:nth-child(1) > div > button > span")).click();
        sleep(3000);
        Set<String> handles = webDriver.getWindowHandles();
        String handle = "";
        for (String x : handles) {
            handle = x;
        }
        webDriver.switchTo().window(handle);
        //获取当前预览页URL
        String curUrl = webDriver.getCurrentUrl();
        System.out.println("curUrl:"+curUrl);
        //只获取URL，去除QueryString部分
        String[] urls = curUrl.split("classifyFile");
        System.out.println("当前预览页面:"+urls[0]);
        Assertions.assertEquals("http://192.168.63.227:10001/digitrain/resource/",urls[0]);
    }


}
